<template>
	<view>
		<view class="content">
			<view class="header">
				<!-- <image src="../../static/image/logo.png"></image> -->
			</view>
			<view class="hide">
				绿源物联设备管理系统
			</view>
			<view class="list">
				<view class="list-call">
					<input class="sl-input" v-model="UserNumber" maxlength="11" placeholder="请输入用户名" />
				</view>
				<view class="list-call">
					<input class="sl-input" v-model="password" type="text" maxlength="15" placeholder="请输入密码"
						password="true" />
				</view>
			</view>
			<view class="button-login btn_shadow" hover-class="button-hover" @tap="bindLogin">
				<text>登录</text>
			</view>
		</view>
	</view>
</template>
<!-- js代码 -->
<script>
	import md5Libs from '../../../static/js/md5.js'
	import {
		index
	} from '../../../api/index.js'
	import {
		mapState
	} from 'vuex'
	let app = getApp()
	let list = []
	export default {
		data() {
			return {
				UserNumber: '', //账号
				password: '', //密码
				// APPAddress: "", //设备码时间戳
			}
		},
		computed: {
			...mapState([
				'token',
				'staffId',
				'status',
			])
		},
		onLoad(e) {
			// this.$fun.showToast(e.id)
		},
		methods: {
			async bindLogin() {
				app.globalData.is_login = true
				if (this.UserNumber.length == 0) {
					this.$fun.showToast('请输入用户名')
					return
				}
				if (this.password.length == 0) {
					this.$fun.showToast('请输入密码')
					return
				}
				// 获得静默登录的code
				// let code = await this.login()

				// 登录传输数据
				let data = {
					username: this.UserNumber, //账号
					password: this.password, //密码
					// APPAddress: this.APPAddress,				//设备码
					// code //用户code
				}
				console.log(data)

				this.$store.dispatch('login', data).then(res => {
					if (res.code == 200) {
						
						console.log('token',this.$store.token)
						// 把用户的密码存入到本地缓存中来
						uni.setStorageSync('token', this.$store.token)
						this.$fun.showToast('登录成功', true)
						this.$store.dispatch('get_user').then((us)=>{
							uni.setStorageSync('user_id', us.data.id)
							uni.setStorageSync('username', us.data.username)
							uni.setStorageSync('photo', us.data.icon)
							setTimeout(function(){
								console.log('icon2',uni.getStorageSync('photo'))
							},1000)
						})
						// this.$store.dispatch('getToken', data)

						uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						this.$fun.showToast(res.msg)
					}
				})
			},
			// 静默登录
			login() {
				return new Promise((resolve, reject) => {
					uni.login({
						success: res => {
							if (res.code) {
								// console.log(res)
								resolve(res.code)
							}
						}
					})
				})
			},

		}
	}
</script>

<style lang="scss">
	@import "index.scss";
	
	page{
		height: 100vh;
		// background-image: url('https://sign.yingdl.com/qrcode/background.jpg');
	}
</style>